package online.shenme.day14_self.controller;

import online.shenme.day14_self.dao.SubwaysDAO;
import online.shenme.day14_self.service.ManageService;

import java.io.IOException;
import java.util.Scanner;

public class Controller {
    Scanner sc = new Scanner(System.in);
    ManageService sv = new ManageService();
    public void manage() {
        while (true) {
            System.out.println("请输入您需要执行的操作：1、查看地铁站信息 2、添加地铁信息 3、保存并退出");
            switch (sc.nextInt()) {
                case 1 -> query();
                case 2 -> putSubway();
                case 3 -> throw new RuntimeException("当前用户已注销，请重新登录!");
                default -> System.out.println("输入有误。请重新输入");
            }
        }
    }

    public void load() throws IOException {
        while (true){
            System.out.println("请输入你的用户ID：");
            String id = sc.next();
            System.out.println("请输入您的用户姓名：");
            String name = sc.next();
            if (sv.load(id, name)) {
                System.out.println("登录成功");
                try {
                    manage();
                } catch (RuntimeException e) {
                    SubwaysDAO.close();
                    System.out.println(e.getMessage());
                }
            } else {
                System.out.println("登录失败，谢谢使用");
//                break;
            }
        }
    }

    public void query() {
        sv.findAll().forEach((s, sub) -> System.out.println(sub.getCode()+"\t"+sub.getName()+"\t"+sub.getBelong()));
    }

    public void putSubway() {
        System.out.println("请输入地铁站内部编号：");
        String code = sc.next();
        System.out.println("请输入地铁站名称：");
        String name = sc.next();
        System.out.println("请输入地铁站所属路线：");
        String belong = sc.next();
        if (sv.put(code, name, belong)) {
            System.out.println("添加成功");
        } else System.out.println("添加失败，请核实");
    }
}
